﻿#define FMT_HEADER_ONLY
#define FMT_UNICODE 0
#include <fmt/core.h>
#include <fmt/ranges.h>

#include <iostream>
#include <sstream>
#include <vector>
#include <iomanip>
#include <stdexcept>

// BER节点定义（支持嵌套结构）
struct BER_Node
{
    std::vector<uint8_t>  tag;            // BER标签（支持多字节）
    size_t                length;         // 数据长度
    bool                  is_container;   // 是否为容器节点（参考BER规范）
    std::vector<uint8_t>  value;
    std::vector<BER_Node> children;

    BER_Node(std::vector<uint8_t> t, size_t l, bool cont) : length(l), is_container(cont)
    {
        tag.swap(t);
    }
};

std::string parseOID(const std::vector<uint8_t>& value)
{
    std::ostringstream oss;
    if (value.empty())
        return "";

    // 解析首字节：X.Y = (40*X)+Y
    uint32_t first = value[0];
    oss << (first / 40) << std::string(".") << (first % 40);

    // 解析后续字节（可变长度整数）
    uint32_t current = 0;
    for (size_t i = 1; i < value.size(); ++i)
    {
        current = (current << 7) | (value[i] & 0x7F);
        if (!(value[i] & 0x80))
        {
            oss << std::string(".") << current;
            current = 0;
        }
    }
    return oss.str();
}

class BER_Parser
{
public:
    BER_Node parse(const std::vector<uint8_t>& data)
    {
        size_t pos = 0;
        return parse_tlv(data, pos);
    }

private:
    /* 标签解析（支持多字节BER标签）
     * 参考BER规范：标签高位表示延续标识
     */
    void parse_tag(const std::vector<uint8_t>& data, size_t& pos, std::vector<uint8_t>& tag)
    {
        if (pos >= data.size())
            throw std::runtime_error("Tag解析越界");
        tag.push_back(data[pos++]);

        if ((tag.at(0) & 0x1F) == 0x1F)
        {
            do 
            {
                if (pos >= data.size())
                    throw std::runtime_error("Tag解析越界");
                tag.push_back(data[pos++]);
            } while ((tag.at(tag.size() - 1) & 0x80) == 0x80);
        }
    }

    /* 长度解析（支持BER不定长格式）
     * 处理规则：
     * 1. 首字节最高位为0：直接长度
     * 2. 首字节最高位为1：多字节编码
     * 3. 0x80表示不定长（当前实现暂不支持）
     */
    size_t parse_length(const std::vector<uint8_t>& data, size_t& pos)
    {
        if (pos >= data.size())
            throw std::runtime_error("长度解析越界");

        uint8_t first = data[pos++];
        if ((first & 0x80) == 0)
            return first;

        int    bytes_count = first & 0x7F;
        size_t length      = 0;
        for (int i = 0; i < bytes_count; ++i)
        {
            if (pos >= data.size())
                throw std::runtime_error("多字节长度越界");
            length = (length << 8) | data[pos++];
        }
        return length;
    }

    // 递归解析入口（核心逻辑）
    BER_Node parse_tlv(const std::vector<uint8_t>& data, size_t& pos)
    {
        std::vector<uint8_t> tag;
        parse_tag(data, pos, tag);

        // 容器判断（BER规范第6位为容器标识）
        bool is_container = (tag.at(0) & 0x20) != 0;

        // 长度解析
        size_t length = parse_length(data, pos);
        if (pos + length > data.size())
        {
            throw std::runtime_error("数据长度越界");
        }

        BER_Node node(tag, length, is_container);


        // 容器节点处理（递归解析子节点）
        if (is_container)
        {
            size_t end_pos = pos + length;
            while (pos < end_pos)
            {
                node.children.push_back(parse_tlv(data, pos));
            }
        }
        else
        {
            // 基础类型直接存储值
            node.value.assign(data.begin() + pos, data.begin() + pos + length);
            pos += length;
        }

        return node;
    }
};

static const char  HEX_CHARS_UPPER[] = "0123456789ABCDEF";
static const char  HEX_CHARS_LOWER[] = "0123456789abcdef";
static std::string to_hex(uint32_t v, bool upper = true)
{
    const uint8_t* data = (const uint8_t*)&v;
    size_t         len  = 1;
    if (v > 0xFFFFFF)
    {
        len = 4;
    }
    else if (v > 0xFFFF)
    {
        len = 3;
    }
    else if (v > 0xFF)
    {
        len = 2;
    }

    const char* hex_chars = upper ? HEX_CHARS_UPPER : HEX_CHARS_LOWER;

    std::string output;
    output.reserve(3 * len);
    for (int i = len-1; i >= 0; i--)
    {
        const unsigned char temp = data[i];
        output.push_back(hex_chars[temp / 16]);
        output.push_back(hex_chars[temp % 16]);
    }

    return output;
}

// 树形结构打印（支持多级缩进）
void print_tree(const BER_Node& node, int depth = 0)
{
    std::string indent(depth * 4, ' ');

    std::cout << indent;
    for (auto byte : node.tag)
    {
        std::cout << std::uppercase << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << "";
    }
    std::cout << " ";
    std::cout << fmt::format("{}", to_hex(node.length));

    if (!node.value.empty())
    {
        std::cout << " ";
        for (auto byte : node.value)
        {
            std::cout << std::uppercase << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << "";
        }
    }
    std::cout << std::endl;

    for (const auto& child : node.children)
    {
        print_tree(child, depth + 1);
    }
}

int ber_tlv()
{
    // 示例数据（包含嵌套结构）
    std::vector<uint8_t> test_data = {
        0x30, 0x82, 0x06, 0xB0, 0x30, 0x82, 0x04, 0x98, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x10, 0x08, 0xAD, 0x40,
        0xB2, 0x60, 0xD2, 0x9C, 0x4C, 0x9F, 0x5E, 0xCD, 0xA9, 0xBD, 0x93, 0xAE, 0xD9, 0x30, 0x0D, 0x06, 0x09, 0x2A,
        0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0C, 0x05, 0x00, 0x30, 0x62, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
        0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x0C,
        0x44, 0x69, 0x67, 0x69, 0x43, 0x65, 0x72, 0x74, 0x20, 0x49, 0x6E, 0x63, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03,
        0x55, 0x04, 0x0B, 0x13, 0x10, 0x77, 0x77, 0x77, 0x2E, 0x64, 0x69, 0x67, 0x69, 0x63, 0x65, 0x72, 0x74, 0x2E,
        0x63, 0x6F, 0x6D, 0x31, 0x21, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x18, 0x44, 0x69, 0x67, 0x69,
        0x43, 0x65, 0x72, 0x74, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74, 0x65, 0x64, 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x20,
        0x47, 0x34, 0x30, 0x1E, 0x17, 0x0D, 0x32, 0x31, 0x30, 0x34, 0x32, 0x39, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
        0x5A, 0x17, 0x0D, 0x33, 0x36, 0x30, 0x34, 0x32, 0x38, 0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5A, 0x30, 0x69,
        0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x17, 0x30, 0x15, 0x06,
        0x03, 0x55, 0x04, 0x0A, 0x13, 0x0E, 0x44, 0x69, 0x67, 0x69, 0x43, 0x65, 0x72, 0x74, 0x2C, 0x20, 0x49, 0x6E,
        0x63, 0x2E, 0x31, 0x41, 0x30, 0x3F, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x38, 0x44, 0x69, 0x67, 0x69, 0x43,
        0x65, 0x72, 0x74, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74, 0x65, 0x64, 0x20, 0x47, 0x34, 0x20, 0x43, 0x6F, 0x64,
        0x65, 0x20, 0x53, 0x69, 0x67, 0x6E, 0x69, 0x6E, 0x67, 0x20, 0x52, 0x53, 0x41, 0x34, 0x30, 0x39, 0x36, 0x20,
        0x53, 0x48, 0x41, 0x33, 0x38, 0x34, 0x20, 0x32, 0x30, 0x32, 0x31, 0x20, 0x43, 0x41, 0x31, 0x30, 0x82, 0x02,
        0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82,
        0x02, 0x0F, 0x00, 0x30, 0x82, 0x02, 0x0A, 0x02, 0x82, 0x02, 0x01, 0x00, 0xD5, 0xB4, 0x2F, 0x42, 0xD0, 0x28,
        0xAD, 0x78, 0xB7, 0x5D, 0xD5, 0x39, 0x59, 0x1B, 0xB1, 0x88, 0x42, 0xF5, 0x33, 0x8C, 0xEB, 0x3D, 0x81, 0x97,
        0x70, 0xC5, 0xBB, 0xC4, 0x85, 0x26, 0x30, 0x9F, 0xA4, 0x8E, 0x68, 0xD8, 0x5C, 0xF5, 0xEB, 0x34, 0x24, 0x07,
        0xE1, 0x4B, 0x4F, 0xD3, 0x78, 0x43, 0xF4, 0x17, 0xD7, 0x1E, 0xDA, 0xF9, 0xD2, 0xD5, 0x67, 0x1A, 0x52, 0x4F,
        0x0E, 0xA1, 0x57, 0xFC, 0x88, 0x99, 0xC1, 0x91, 0xCC, 0x81, 0x03, 0x3E, 0x4D, 0x70, 0x24, 0x64, 0xB3, 0x8D,
        0xE2, 0x08, 0x7D, 0x34, 0x7D, 0x4C, 0x80, 0x57, 0x12, 0x6B, 0x43, 0x9A, 0x99, 0xF2, 0xC5, 0x3B, 0x1F, 0xF2,
        0xEF, 0xCB, 0x47, 0x5A, 0x13, 0xA6, 0x4C, 0xB3, 0x01, 0x20, 0x25, 0xF3, 0x10, 0xD3, 0x8B, 0xB2, 0xFB, 0x08,
        0xF0, 0x8A, 0xE0, 0x9D, 0x09, 0xC0, 0x65, 0xA7, 0xFA, 0x98, 0x80, 0x49, 0x35, 0x87, 0x3D, 0x51, 0x19, 0xE8,
        0x90, 0x21, 0x78, 0x45, 0x2E, 0xA1, 0x9F, 0x2C, 0xE1, 0x18, 0xC2, 0x1A, 0xCC, 0xC5, 0xEE, 0x93, 0x49, 0x70,
        0x42, 0x32, 0x8F, 0xFB, 0xC6, 0xEA, 0x1C, 0xF3, 0x65, 0x68, 0x91, 0xA2, 0x4D, 0x4C, 0x82, 0x11, 0x48, 0x52,
        0x68, 0xDE, 0x10, 0xBD, 0x14, 0x57, 0x5D, 0xE8, 0x18, 0x13, 0x65, 0xC5, 0x7F, 0xB2, 0x4F, 0x85, 0x2C, 0x48,
        0xA4, 0x56, 0x84, 0x35, 0xD6, 0xF9, 0x2E, 0x9C, 0xAA, 0x00, 0x15, 0xD1, 0x37, 0xFE, 0x1A, 0x06, 0x94, 0xC2,
        0x7C, 0xC8, 0xEA, 0x1B, 0x32, 0xE6, 0xCA, 0xC2, 0xF4, 0xA7, 0xA3, 0x03, 0x0E, 0x74, 0xA5, 0xAF, 0x39, 0xB6,
        0xAB, 0x60, 0x12, 0xE3, 0xE8, 0xD6, 0xB9, 0xF7, 0x31, 0xE1, 0xDC, 0xAD, 0xE4, 0x18, 0xA0, 0xD8, 0xC1, 0x23,
        0x47, 0x47, 0xB3, 0xA1, 0x0F, 0x6E, 0xA3, 0xAB, 0x6D, 0x98, 0x06, 0x83, 0x1B, 0xB7, 0x6A, 0x67, 0x2D, 0xD2,
        0xBD, 0x44, 0x1A, 0x92, 0x10, 0x81, 0x8F, 0xB0, 0x3B, 0x09, 0xD7, 0xC7, 0x9B, 0x32, 0x5A, 0xC2, 0xFF, 0x6A,
        0x60, 0x54, 0x8B, 0x49, 0xC1, 0x93, 0xED, 0xE1, 0xB4, 0x5C, 0xE0, 0x6F, 0xEB, 0x26, 0xF9, 0x8C, 0xD5, 0xB2,
        0xF9, 0x38, 0x10, 0xE6, 0xEA, 0xCE, 0x91, 0xF5, 0xBE, 0xD3, 0xFB, 0x6F, 0x93, 0x61, 0x34, 0x5C, 0xBC, 0x93,
        0x45, 0x28, 0x83, 0x36, 0x2A, 0x66, 0x28, 0x5F, 0xB0, 0x73, 0xCE, 0x8B, 0x26, 0x25, 0x06, 0xB2, 0x83, 0xD4,
        0x5C, 0xF6, 0x15, 0x19, 0x4C, 0xED, 0x62, 0xE0, 0x5E, 0x33, 0xF2, 0xE8, 0xE8, 0xEC, 0x0A, 0xA7, 0xB0, 0x03,
        0x2B, 0x91, 0xB2, 0x36, 0x79, 0xBE, 0xF7, 0xAD, 0x08, 0x1E, 0x75, 0xA6, 0x65, 0xCC, 0xBB, 0xE3, 0x48, 0x50,
        0xF3, 0x77, 0x91, 0x1A, 0xFE, 0xDB, 0x50, 0xA2, 0x46, 0xC8, 0x61, 0x58, 0x98, 0xF5, 0x7C, 0x02, 0x16, 0x3C,
        0x83, 0x28, 0xAD, 0x39, 0x86, 0xEC, 0xD4, 0xB7, 0x0D, 0x53, 0xD0, 0xF8, 0x47, 0xE6, 0x75, 0x30, 0x8D, 0xEC,
        0x30, 0x93, 0x76, 0x14, 0xA6, 0x5B, 0x4B, 0x5D, 0x74, 0x61, 0x4D, 0x3F, 0x12, 0x91, 0x76, 0xDE, 0xBF, 0x58,
        0xCB, 0x72, 0x10, 0x29, 0x41, 0xF0, 0xD5, 0xC5, 0x6D, 0x26, 0x76, 0x68, 0x11, 0x41, 0x13, 0x58, 0x9A, 0xDC,
        0x26, 0x2B, 0x01, 0xF4, 0x89, 0x4D, 0x59, 0xDB, 0x78, 0xCF, 0x81, 0x4A, 0x3E, 0x40, 0x47, 0x5F, 0xC9, 0x81,
        0x50, 0x73, 0x85, 0x10, 0x23, 0x21, 0x59, 0x60, 0x8A, 0x64, 0x54, 0xC1, 0xCC, 0x21, 0x1A, 0xE8, 0x38, 0x19,
        0x7C, 0x66, 0x1C, 0xCD, 0x78, 0x38, 0x45, 0x30, 0x99, 0x4F, 0xFF, 0x63, 0x4F, 0x4C, 0xBB, 0xAA, 0x0D, 0x08,
        0x53, 0x41, 0x7C, 0x58, 0x3D, 0x47, 0xB3, 0xFA, 0xB6, 0xEC, 0x8C, 0x32, 0x09, 0x02, 0xCC, 0x6C, 0x3C, 0x0C,
        0x56, 0x11, 0x02, 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x59, 0x30, 0x82, 0x01, 0x55, 0x30, 0x12, 0x06,
        0x03, 0x55, 0x1D, 0x13, 0x01, 0x01, 0xFF, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xFF, 0x02, 0x01, 0x00, 0x30,
        0x1D, 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x68, 0x37, 0xE0, 0xEB, 0xB6, 0x3B, 0xF8, 0x5F,
        0x11, 0x86, 0xFB, 0xFE, 0x61, 0x7B, 0x08, 0x88, 0x65, 0xF4, 0x4E, 0x42, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x1D,
        0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xEC, 0xD7, 0xE3, 0x82, 0xD2, 0x71, 0x5D, 0x64, 0x4C, 0xDF, 0x2E,
        0x67, 0x3F, 0xE7, 0xBA, 0x98, 0xAE, 0x1C, 0x0F, 0x4F, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x1D, 0x0F, 0x01, 0x01,
        0xFF, 0x04, 0x04, 0x03, 0x02, 0x01, 0x86, 0x30, 0x13, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x0C, 0x30, 0x0A,
        0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x03, 0x30, 0x77, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05,
        0x05, 0x07, 0x01, 0x01, 0x04, 0x6B, 0x30, 0x69, 0x30, 0x24, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
        0x30, 0x01, 0x86, 0x18, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x6F, 0x63, 0x73, 0x70, 0x2E, 0x64, 0x69,
        0x67, 0x69, 0x63, 0x65, 0x72, 0x74, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x41, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05,
        0x05, 0x07, 0x30, 0x02, 0x86, 0x35, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x61, 0x63, 0x65, 0x72,
        0x74, 0x73, 0x2E, 0x64, 0x69, 0x67, 0x69, 0x63, 0x65, 0x72, 0x74, 0x2E, 0x63, 0x6F, 0x6D, 0x2F, 0x44, 0x69,
        0x67, 0x69, 0x43, 0x65, 0x72, 0x74, 0x54, 0x72, 0x75, 0x73, 0x74, 0x65, 0x64, 0x52, 0x6F, 0x6F, 0x74, 0x47,
        0x34, 0x2E, 0x63, 0x72, 0x74, 0x30, 0x43, 0x06, 0x03, 0x55, 0x1D, 0x1F, 0x04, 0x3C, 0x30, 0x3A, 0x30, 0x38,
        0xA0, 0x36, 0xA0, 0x34, 0x86, 0x32, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x72, 0x6C, 0x33, 0x2E,
        0x64, 0x69, 0x67, 0x69, 0x63, 0x65, 0x72, 0x74, 0x2E, 0x63, 0x6F, 0x6D, 0x2F, 0x44, 0x69, 0x67, 0x69, 0x43,
        0x65, 0x72, 0x74, 0x54, 0x72, 0x75, 0x73, 0x74, 0x65, 0x64, 0x52, 0x6F, 0x6F, 0x74, 0x47, 0x34, 0x2E, 0x63,
        0x72, 0x6C, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x20, 0x04, 0x15, 0x30, 0x13, 0x30, 0x07, 0x06, 0x05, 0x67,
        0x81, 0x0C, 0x01, 0x03, 0x30, 0x08, 0x06, 0x06, 0x67, 0x81, 0x0C, 0x01, 0x04, 0x01, 0x30, 0x0D, 0x06, 0x09,
        0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0C, 0x05, 0x00, 0x03, 0x82, 0x02, 0x01, 0x00, 0x3A, 0x23,
        0x44, 0x3D, 0x8D, 0x08, 0x76, 0xEE, 0x8F, 0xBC, 0x3A, 0x99, 0xD3, 0x56, 0xE0, 0x02, 0x1A, 0xA5, 0xF8, 0x48,
        0x34, 0xF3, 0x2C, 0xB6, 0xE6, 0x74, 0x66, 0xF7, 0x94, 0x72, 0xB1, 0x00, 0xCA, 0xAF, 0x6C, 0x30, 0x27, 0x13,
        0x12, 0x9E, 0x90, 0x44, 0x9F, 0x4B, 0xFD, 0x9E, 0xA3, 0x7C, 0x26, 0xD5, 0x37, 0xBC, 0x3A, 0x5D, 0x48, 0x6D,
        0x95, 0xD5, 0x3F, 0x49, 0xF4, 0x27, 0xBB, 0x16, 0x81, 0x45, 0x50, 0xFD, 0x9C, 0xBD, 0xB6, 0x85, 0xE0, 0x76,
        0x7E, 0x37, 0x71, 0xCB, 0x22, 0xF7, 0x5A, 0xAA, 0x90, 0xCF, 0xF5, 0x93, 0x6A, 0xE3, 0xEB, 0x20, 0xD1, 0xD5,
        0x50, 0x79, 0x88, 0x9A, 0x8A, 0x8A, 0xC1, 0xB6, 0xBD, 0xA1, 0x48, 0x18, 0x7E, 0xDC, 0xD8, 0x80, 0x1A, 0x11,
        0x19, 0x18, 0xCD, 0x61, 0x99, 0x81, 0x56, 0xF6, 0xC9, 0xE3, 0x76, 0xE7, 0xC4, 0xE4, 0x1B, 0x5F, 0x43, 0xF8,
        0x3E, 0x94, 0xFF, 0x76, 0x39, 0x3D, 0x9E, 0xD4, 0x99, 0xCF, 0x4A, 0xDD, 0x28, 0xEB, 0x5F, 0x26, 0xA1, 0x95,
        0x58, 0x48, 0xD5, 0x1A, 0xFE, 0xD7, 0x27, 0x3F, 0xFD, 0x90, 0xD1, 0x76, 0x86, 0xDD, 0x1C, 0xB0, 0x60, 0x5C,
        0xF3, 0x0D, 0xA8, 0xEE, 0xE0, 0x89, 0xA1, 0xBD, 0x39, 0xE1, 0x38, 0x4E, 0xDA, 0x6E, 0xBB, 0x36, 0x9D, 0xFB,
        0xE5, 0x21, 0x53, 0x5A, 0xC3, 0xCA, 0xE9, 0x6A, 0xF1, 0xA2, 0x3E, 0xDB, 0x43, 0xB8, 0x33, 0xC8, 0x4F, 0x38,
        0x14, 0x92, 0x99, 0xF5, 0xDD, 0xCE, 0x54, 0x6D, 0xD9, 0x5D, 0x02, 0x14, 0x1F, 0x40, 0x33, 0x7C, 0x03, 0xE2,
        0x95, 0xB2, 0xC2, 0x21, 0x75, 0x73, 0x52, 0xCB, 0x46, 0xD8, 0xC4, 0x34, 0x1C, 0xA2, 0xA5, 0x4B, 0x8D, 0xCD,
        0x6F, 0x76, 0x37, 0x2C, 0x85, 0x3F, 0x1A, 0xCE, 0x26, 0xE9, 0x18, 0xBE, 0x90, 0x07, 0xB0, 0x43, 0x7F, 0x95,
        0x88, 0x20, 0x82, 0x70, 0xF0, 0xCC, 0xCA, 0xEF, 0xFD, 0x29, 0x35, 0x5C, 0x1F, 0x89, 0x38, 0x55, 0xF7, 0x37,
        0x8A, 0x8B, 0x09, 0xA1, 0xCB, 0x0B, 0xE9, 0x31, 0x1A, 0xFF, 0x2E, 0x19, 0x5C, 0x39, 0x71, 0xE1, 0xBE, 0x9C,
        0xA7, 0x0A, 0x06, 0xD6, 0x26, 0x67, 0xB7, 0x92, 0xE6, 0x4E, 0x5F, 0xDE, 0x7A, 0xAC, 0x49, 0xCF, 0x2E, 0xA4,
        0x74, 0x92, 0xAD, 0xDB, 0x3C, 0xA4, 0x9C, 0x86, 0x1F, 0xE3, 0xC1, 0x56, 0x1B, 0x2B, 0x23, 0xFF, 0x8F, 0xB5,
        0xEA, 0x88, 0x7B, 0x70, 0x6B, 0xE6, 0xA0, 0xBA, 0xFD, 0x3A, 0x3F, 0x45, 0xA6, 0xC4, 0xE8, 0x16, 0x91, 0x52,
        0x8B, 0x41, 0xC0, 0x48, 0x84, 0x4B, 0x96, 0x4D, 0xAB, 0x44, 0x40, 0xE3, 0x8D, 0xF0, 0x15, 0x28, 0xCE, 0xED,
        0xF1, 0x18, 0x56, 0x07, 0x2A, 0x2F, 0x10, 0xC4, 0x0C, 0x08, 0x64, 0x3C, 0x33, 0x8F, 0xAE, 0x28, 0x8C, 0x3C,
        0xCB, 0x8F, 0x88, 0x0B, 0x0D, 0xBF, 0x3B, 0xF4, 0xCE, 0x1E, 0x7B, 0x8E, 0xEF, 0xB5, 0xEB, 0xCB, 0xB7, 0xF0,
        0x77, 0x13, 0xE6, 0xE7, 0x28, 0x3F, 0xAC, 0x12, 0xAE, 0xA5, 0x2F, 0x22, 0x6C, 0x41, 0xF9, 0x82, 0x5C, 0x15,
        0x66, 0xCC, 0x6C, 0x0E, 0xCA, 0xC5, 0x86, 0xC3, 0xF6, 0x26, 0x33, 0x0C, 0x07, 0x4B, 0xA0, 0xD3, 0x07, 0x02,
        0x6A, 0x6A, 0x40, 0x30, 0x48, 0x4B, 0x34, 0xA8, 0x51, 0x20, 0xBB, 0xAD, 0x1B, 0x85, 0x08, 0xE2, 0x59, 0x0D,
        0x6D, 0xCA, 0x05, 0x50, 0x2B, 0xEA, 0x4A, 0x1C, 0x9E, 0xA5, 0xFD, 0xA0, 0xA7, 0x1F, 0x06, 0x74, 0xE7, 0xF2,
        0xD6, 0x52, 0x90, 0xFD, 0xAF, 0x85, 0x48, 0x21, 0xF9, 0x57, 0x3B, 0xB4, 0x9C, 0x03, 0xED, 0x86, 0x45, 0xF4,
        0xB4, 0x61, 0x6E, 0xBF, 0x68, 0xE2, 0x26, 0x60, 0x86, 0xEA, 0xC8, 0xAF, 0xA9, 0xFE, 0x94, 0x1D, 0xE7, 0x63,
        0x1B, 0x3A, 0x86, 0x56, 0x78, 0x4E, 
    };

    try
    {
        BER_Parser parser;
        BER_Node   root = parser.parse(test_data);
        print_tree(root);

        std::vector<uint8_t> test2 = {0x6F, 0x30, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E,
                                      0x44, 0x44, 0x46, 0x30, 0x31, 0xA5, 0x1E, 0xBF, 0x0C, 0x1B, 0x61, 0x19, 0x4F,
                                      0x08, 0xA0, 0x00, 0x00, 0x03, 0x33, 0x01, 0x01, 0x01, 0x50, 0x0A, 0x50, 0x42,
                                      0x4F, 0x43, 0x20, 0x44, 0x45, 0x42, 0x49, 0x54, 0x87, 0x01, 0x01};

        root = parser.parse(test2);
        print_tree(root);

        std::vector<uint8_t> test3 = {0x9F, 0x70, 0x01, 0x01};

        root = parser.parse(test3);
        print_tree(root);

        std::vector<uint8_t> test4 = {0x9F, 0x9F, 0x02, 0x02, 0x01, 0x01};

        root = parser.parse(test4);
        print_tree(root);

        std::vector<uint8_t> test5 = {0x9F, 0x9F, 0x80, 0x01, 0x02, 0x01, 0x01};

        root = parser.parse(test5);
        print_tree(root);
    }
    catch (const std::exception& e)
    {
        std::cerr << "解析错误: " << e.what() << std::endl;
        return 1;
    }
    return 0;
}